package com.CMS.client.ui.view;

import java.util.LinkedHashMap;
import java.util.List;

import com.CMS.client.LanguageService;
import com.CMS.client.LanguageServiceAsync;
import com.CMS.client.NameTokens;
import com.CMS.client.UrlManagement;
import com.CMS.client.UserService;
import com.CMS.client.UserServiceAsync;
import com.CMS.server.domain.Language;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class SettingLanguageView extends VLayout
{
    private static final String DESCRIPTION = "SettingLanguageView";
	private LinkedHashMap<String, String> hash = new LinkedHashMap<String, String>();
	
	public SettingLanguageView()
	 {
		  super();
		    
		    final DynamicForm form = new DynamicForm();  
		    form.setWidth(450); 
		    form.setHeight(110);
	        form.setTitleOrientation(TitleOrientation.TOP);
	        form.setTop(200);
	        
	        final RadioGroupItem radioGroupItem = new RadioGroupItem();  
	        radioGroupItem.setName("willAttend");  
	        radioGroupItem.setColSpan("*");  
	        radioGroupItem.setRequired(true);  
	        radioGroupItem.setVertical(true);  
	        LanguageServiceAsync rpcLang = GWT.create(LanguageService.class);
	        rpcLang.getAll(new AsyncCallback<List<Language>>() {
				
				@Override
				public void onSuccess(List<Language> result) {					
					for(int i=0; i<result.size(); i++){
						hash.put(result.get(i).getValue(),result.get(i).getDescription());
					}					
					UserServiceAsync rpcUser = GWT.create(UserService.class);
					rpcUser.getLanguage(Cookies.getCookie(NameTokens.USSER_SESSION), new AsyncCallback<Language>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Language result) {
							radioGroupItem.setValueMap(hash);
							radioGroupItem.setDefaultValue(result.getValue());
							radioGroupItem.redraw();
						}
					});					
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}
			}); 
	        radioGroupItem.setRedrawOnChange(true);     
	        radioGroupItem.setTitle("Seleccione Idioma a configurar");  
	  
	        ButtonItem buttonItem = new ButtonItem("validate", "&nbsp;Guardar&nbsp;");  
	        buttonItem.setIcon("icons/16/settingLanguage.png");
	        buttonItem.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {					
					final String valueStr = (String) radioGroupItem.getValue();					
					UserServiceAsync rpc = GWT.create(UserService.class);
					rpc.setLanguage(Cookies.getCookie(NameTokens.USSER_SESSION), valueStr, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Void result){
							NameTokens.SET_LANGUAGE = valueStr;	
				            Cookies.setCookie(NameTokens.DEFAULT_PAGE,UrlManagement.URL);
							Window.Location.replace(UrlManagement.URL);						
					   }
					});
				}
			});
	        
	  
	        form.setFields(radioGroupItem,buttonItem);  
	        form.setBorder("4px solid #C0C3C7");
	        form.setPadding(10);
	        form.setMargin(80);
	        
		    final HLayout hLayoutAlignCenter = new HLayout();  
	    
	        hLayoutAlignCenter.setWidth100();  
	        hLayoutAlignCenter.setHeight100();  
	        hLayoutAlignCenter.setLayoutMargin(10);  
	        hLayoutAlignCenter.setMembersMargin(40);   
	        hLayoutAlignCenter.setBorder("1px solid #C0C3C7");
	        hLayoutAlignCenter.setAlign(Alignment.CENTER); // As promised!  
	          
	        hLayoutAlignCenter.addMember(form);   
	        
		    this.addMember(hLayoutAlignCenter);
		    this.setLayoutMargin(2);  
	        this.setMembersMargin(2); 
	 }
	 
	 public static class Factory implements ContextAreaFactory
	 {
		 private String id;
		 
		 public Canvas create()
		 {
	      //hay que crear la vista que vamos a llamar
		  SettingLanguageView view = new SettingLanguageView();
		  id = view.getID();
	      return view;
	     }
		 
		 public String getID()
		 {
		   return id;
		 }

		 public String getDescription() 
		 {
		   return DESCRIPTION;
		 }
	 }
}
